﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Configuration;

namespace SharePoint.Log.Viewer._2010
{
    public class Utils
    {
        string defaultLOGSPath = ConfigurationManager.AppSettings["LOGSPath"];

        public Utils()
        {
            //setup percorso
        }

        public List<KeyValuePair<string, string>> GetLOGSFile()
        {
            KeyValuePair<string, string> file;
            List<KeyValuePair<string, string>> files = new List<KeyValuePair<string, string>>();
            try
            {
                DirectoryInfo directory = new DirectoryInfo(defaultLOGSPath);
                if (!directory.Exists)
                {
                    throw new Exception("Directory " + defaultLOGSPath + " not exist!");
                }
                else
                {
                    foreach (var item in directory.GetFiles("*.log").OrderByDescending(d => d.LastWriteTime))
                    {
                        file = new KeyValuePair<string, string>(item.FullName, item.Name);
                        files.Add(file);
                    }
                }

                return files;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public List<Content> GetContent(string file)
        {
            FileStream fs = new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);

            try
            {
                List<Content> contents = new List<Content>();
                Content content = null;

                using (StreamReader sr = new StreamReader(fs))
                {
                    while (!sr.EndOfStream)
                    {
                        string str = sr.ReadLine();
                        if (str.Length > 0)
                        {
                            string[] line = str.Split('\t');
                            DateTime timestamp;
                            if (DateTime.TryParse(line[0].Trim(), out timestamp))
                            {
                                content = new Content();
                                content.Timestamp = timestamp;
                                content.Process = line[1].Trim();
                                content.TID = line[2].Trim();
                                content.Area = line[3].Trim();
                                content.Category = line[4].Trim();
                                content.EventID = line[5].Trim();
                                content.Level = line[6].Trim();
                                content.Message = line[7].Trim();
                                content.Guid = (string.IsNullOrEmpty(line[8].Trim()) ? Guid.Empty : new Guid(line[8].Trim()));
                                contents.Add(content);
                            }   
                        }
                    }
                };
                return contents;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                fs.Close();
            }
        }

    }

    public class Content
    {
        public DateTime Timestamp { get; set; }
        public string Process { get; set; }
        public string TID { get; set; }
        public string Area { get; set; }
        public string Category { get; set; }
        public string EventID { get; set; }
        public string Level { get; set; }
        public string Message { get; set; }
        public Guid Guid { get; set; }
    }
}
